package com.ll.setdemo;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 刘龙
 * @version 1.0
 */
public class HashSetDemo02 {
    public static void main(String[] args) {
        /**
         * 底层是 HashMap
         *     public HashSet() {
         *         map = new HashMap<>();
         *     }
         */
        HashSet hashSet = new HashSet();

        /**
         *     public boolean add(E e) {
         *         return map.put(e, PRESENT)==null;
         *     }
         */
        System.out.println(hashSet.add("java"));
        System.out.println(hashSet.add("spring"));
        // false
        System.out.println(hashSet.add("java"));
        System.out.println(hashSet.add("python"));
        System.out.println(hashSet.add("Vue"));
        hashSet.remove("java");
        // [spring, python, Vue]
        System.out.println(hashSet);

        System.out.println("=========================");

        hashSet = new HashSet();
        // hashSet= []
        System.out.println("hashSet= " + hashSet);

        // hashSet 不能添加相同的元素/数据
        hashSet.add("lucy");
        hashSet.add("lucy");
        hashSet.add(new Dog("tom"));
        hashSet.add(new Dog("tom"));
        // hashSet= [Dog{name='tom'}, Dog{name='tom'}, lucy]
        System.out.println("hashSet= " + hashSet);

        System.out.println("=========================");
        // 非常经典的面试题
        hashSet.add(new String("ll"));
        hashSet.add(new String("ll"));
        // [ll, Dog{name='tom'}, Dog{name='tom'}, lucy]
        System.out.println(hashSet);
    }
}

class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                '}';
    }
}
